<template>
  <div class="login-container">
    <div class="login-left">
      <img src="../assets/img/login_img.png" alt="" />
    </div>
    <div class="login-box">
      <!-- 头像 -->
      <div class="avatar-box">
        <img src="../assets/img/login_logo.png" alt="" />
      </div>
      <!-- 标题 -->
      <div class="title">电商后台管理系统</div>
      <!-- 登录表单 -->
      <el-form ref="loginFormRef" class="login-form" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" prefix-icon="iconfont icon-user" v-model="loginForm.username" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" show-password prefix-icon="iconfont icon-lock_fill" v-model="loginForm.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 10, message: "长度应该保持在5-10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度应该保持在6-12个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        }
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status != 200) {
          return this.$message.error("boys，你登录失败了");
        } else {
          this.$message.success("登录成功");
          sessionStorage.setItem("userInfo", JSON.stringify(res.data));
          this.$router.push("/home");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  background: #93defe;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-left {
    margin-right: 100px;
  }
  .login-box {
    width: 400px;
    height: 350px;
    background: #fff;
    padding: 50px;
    border-radius: 6px;
    box-sizing: border-box;
    position: relative;
    .avatar-box {
      width: 120px;
      height: 120px;
      border: 5px solid #93defe;
      border-radius: 100%;
      text-align: center;
      line-height: 110px;
      position: absolute;
      top: -60px;
      right: 140px;
      background: #fff;
    }
    .title {
      position: absolute;
      top: 100px;
      left: 50%;
      transform: translate(-50%);
      font-size: 18px;
      color: #444;
    }
    .login-form {
      position: absolute;
      bottom: 25px;
      left: 50%;
      transform: translate(-50%);
      padding: 0 20px;
      width: 80%;
      box-sizing: border-box;
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
